home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / lpd / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-16  |  6.3 KB  |  254 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)common.c    5.3 (Berkeley) 5/5/88";
  15. #endif /* not lint */
  16.  
  17. /*
  18.  * Routines and data common to all the line printer functions.
  19.  */
  20.  
  21. #include "lp.h"
  22.  
  23. int    DU;        /* daeomon user-id */
  24. int    MX;        /* maximum number of blocks to copy */
  25. int    MC;        /* maximum number of copies allowed */
  26. char    *LP;        /* line printer device name */
  27. char    *RM;        /* remote machine name */
  28. char    *RP;        /* remote printer name */
  29. char    *LO;        /* lock file name */
  30. char    *ST;        /* status file name */
  31. char    *SD;        /* spool directory */
  32. char    *AF;        /* accounting file */
  33. char    *LF;        /* log file for error messages */
  34. char    *OF;        /* name of output filter (created once) */
  35. char    *IF;        /* name of input filter (created per job) */
  36. char    *RF;        /* name of fortran text filter (per job) */
  37. char    *TF;        /* name of troff filter (per job) */
  38. char    *NF;        /* name of ditroff filter (per job) */
  39. char    *DF;        /* name of tex filter (per job) */
  40. char    *GF;        /* name of graph(1G) filter (per job) */
  41. char    *VF;        /* name of vplot filter (per job) */
  42. char    *CF;        /* name of cifplot filter (per job) */
  43. char    *PF;        /* name of vrast filter (per job) */
  44. char    *FF;        /* form feed string */
  45. char    *TR;        /* trailer string to be output when Q empties */
  46. short    SC;        /* suppress multiple copies */
  47. short    SF;        /* suppress FF on each print job */
  48. short    SH;        /* suppress header page */
  49. short    SB;        /* short banner instead of normal header */
  50. short    HL;        /* print header last */
  51. short    RW;        /* open LP for reading and writing */
  52. short    PW;        /* page width */
  53. short    PL;        /* page length */
  54. short    PX;        /* page width in pixels */
  55. short    PY;        /* page length in pixels */
  56. short    BR;        /* baud rate if lp is a tty */
  57. int    FC;        /* flags to clear if lp is a tty */
  58. int    FS;        /* flags to set if lp is a tty */
  59. int    XC;        /* flags to clear for local mode */
  60. int    XS;        /* flags to set for local mode */
  61. short    RS;        /* restricted to those with local accounts */
  62.  
  63. char    line[BUFSIZ];
  64. char    pbuf[BUFSIZ/2];    /* buffer for printcap strings */
  65. char    *bp = pbuf;    /* pointer into pbuf for pgetent() */
  66. char    *name;        /* program name */
  67. char    *printer;    /* printer name */
  68. char    host[32];    /* host machine name */
  69. char    *from = host;    /* client's machine name */
  70.  
  71. static int compar();
  72.  
  73. /*
  74.  * Create a connection to the remote printer server.
  75.  * Most of this code comes from rcmd.c.
  76.  */
  77. getport(rhost)
  78.     char *rhost;
  79. {
  80.     struct hostent *hp;
  81.     struct servent *sp;
  82.     struct sockaddr_in sin;
  83.     int s, timo = 1, lport = IPPORT_RESERVED - 1;
  84.     int err;
  85.     extern int debug;
  86.  
  87.     /*
  88.      * Get the host address and port number to connect to.
  89.      */
  90.     if (rhost == NULL)
  91.         fatal("no remote host to connect to");
  92.     hp = gethostbyname(rhost);
  93.     if (hp == NULL)
  94.         fatal("unknown host %s", rhost);
  95.     sp = getservbyname("printer", "tcp");
  96.     if (sp == NULL)
  97.         fatal("printer/tcp: unknown service");
  98.     bzero((char *)&sin, sizeof(sin));
  99.     bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
  100.     sin.sin_family = hp->h_addrtype;
  101.     sin.sin_port = sp->s_port;
  102.  
  103.     /*
  104.      * Try connecting to the server.
  105.      */
  106. retry:
  107.     s = rresvport(&lport);
  108.     if (s < 0) {
  109.             if (debug) {
  110.             syslog("rresvport: %s\n", strerror(errno));
  111.         }
  112.         return(-1);
  113.     }
  114.     if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
  115.         err = errno;
  116.         (void) close(s);
  117.         errno = err;
  118.         if (errno == EADDRINUSE) {
  119.             lport--;
  120.             goto retry;
  121.         }
  122.         if (errno == ECONNREFUSED && timo <= 16) {
  123.             sleep(timo);
  124.             timo *= 2;
  125.             goto retry;
  126.         }
  127.             if (debug) {
  128.             syslog("connect: %s\n", strerror(errno));
  129.         }
  130.         return(-1);
  131.     }
  132.     return(s);
  133. }
  134.  
  135. /*
  136.  * Getline reads a line from the control file cfp, removes tabs, converts
  137.  *  new-line to null and leaves it in line.
  138.  * Returns 0 at EOF or the number of characters read.
  139.  */
  140. getline(cfp)
  141.     FILE *cfp;
  142. {
  143.     register int linel = 0;
  144.     register char *lp = line;
  145.     register c;
  146.  
  147.     while ((c = getc(cfp)) != '\n') {
  148.         if (c == EOF)
  149.             return(0);
  150.         if (c == '\t') {
  151.             do {
  152.                 *lp++ = ' ';
  153.                 linel++;
  154.             } while ((linel & 07) != 0);
  155.             continue;
  156.         }
  157.         *lp++ = c;
  158.         linel++;
  159.     }
  160.     *lp++ = '\0';
  161.     return(linel);
  162. }
  163.  
  164. /*
  165.  * Scan the current directory and make a list of daemon files sorted by
  166.  * creation time.
  167.  * Return the number of entries and a pointer to the list.
  168.  */
  169. getq(namelist)
  170.     struct queue *(*namelist[]);
  171. {
  172.     register struct direct *d;
  173.     register struct queue *q, **queue;
  174.     register int nitems;
  175.     struct stat stbuf;
  176.     int arraysz;
  177.     DIR *dirp;
  178.  
  179.     if ((dirp = opendir(SD)) == NULL)
  180.         return(-1);
  181.     if (fstat(dirp->dd_fd, &stbuf) < 0)
  182.         goto errdone;
  183.  
  184.     /*
  185.      * Estimate the array size by taking the size of the directory file
  186.      * and dividing it by a multiple of the minimum size entry. 
  187.      */
  188.     arraysz = (stbuf.st_size / 24);
  189.     queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
  190.     if (queue == NULL)
  191.         goto errdone;
  192.  
  193.     nitems = 0;
  194.     while ((d = readdir(dirp)) != NULL) {
  195.         if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
  196.             continue;    /* daemon control files only */
  197.         if (stat(d->d_name, &stbuf) < 0)
  198.             continue;    /* Doesn't exist */
  199.         q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
  200.         if (q == NULL)
  201.             goto errdone;
  202.         q->q_time = stbuf.st_mtime;
  203.         strcpy(q->q_name, d->d_name);
  204.         /*
  205.          * Check to make sure the array has space left and
  206.          * realloc the maximum size.
  207.          */
  208.         if (++nitems > arraysz) {
  209.             queue = (struct queue **)realloc((char *)queue,
  210.                 (stbuf.st_size/12) * sizeof(struct queue *));
  211.             if (queue == NULL)
  212.                 goto errdone;
  213.         }
  214.         queue[nitems-1] = q;
  215.     }
  216.     closedir(dirp);
  217.     if (nitems)
  218.         qsort(queue, nitems, sizeof(struct queue *), compar);
  219.     *namelist = queue;
  220.     return(nitems);
  221.  
  222. errdone:
  223.     closedir(dirp);
  224.     return(-1);
  225. }
  226.  
  227. /*
  228.  * Compare modification times.
  229.  */
  230. static int
  231. compar(p1, p2)
  232.     register struct queue **p1, **p2;
  233. {
  234.     if ((*p1)->q_time < (*p2)->q_time)
  235.         return(-1);
  236.     if ((*p1)->q_time > (*p2)->q_time)
  237.         return(1);
  238.     return(0);
  239. }
  240.  
  241. /*VARARGS1*/
  242. fatal(msg, a1, a2, a3)
  243.     char *msg;
  244. {
  245.     if (from != host)
  246.         printf("%s: ", host);
  247.     printf("%s: ", name);
  248.     if (printer)
  249.         printf("%s: ", printer);
  250.     printf(msg, a1, a2, a3);
  251.     putchar('\n');
  252.     exit(1);
  253. }
  254.